package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        //1.效验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取登录用户，判断是否登录
        ApUser apUser = ApThreadLocalUtil.getUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行以下操作
        if (apUser.getId() == null) {
            return ResponseResult.okResult(405,"游客身份禁止，需进行登录");
        }
        //3.判断用户是否已经不喜欢，查询redis
        String unlike_hash_key = "USER_UNLIKES" + dto.getArticleId();
        //3.1判断是已经不喜欢：查询关注缓存中的小key是否包含点赞人id
        Boolean hasKey = redisTemplate.opsForHash().hasKey(unlike_hash_key, apUser.getId()+"");
        //如果已经不喜欢，并且用户还在执行此操作，则返回
        if (hasKey && dto.getType() == 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //如果已经不喜欢，并且执行取消不喜欢操作
        if (hasKey && dto.getType() == 1) {
            //删除redis数据
            redisTemplate.opsForHash().delete(unlike_hash_key, apUser.getId() + "");
            //删除mongodb数据
            mongoTemplate.remove(
                    Query.query(Criteria.where("userId").is(apUser.getId()).and("articleId").is(dto.getArticleId()))
            , ApUnlikesBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //用户第一次点击不喜欢操作
        if (!hasKey && dto.getType()==0) {
            //存储到redis中
            redisTemplate.opsForHash().put(unlike_hash_key,apUser.getId() + "","1");
            //存储到MongoDB中
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setId(ObjectId.get().toHexString());
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setType((short) 0);
            mongoTemplate.insert(apUnlikesBehavior);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
